package com.ltxtiyu.video.tool;

import net.sf.jmimemagic.Magic;
import net.sf.jmimemagic.MagicMatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

/**
 * 
 * JMimeMagic是一个通过检查magic headers来判断文件或者流的mime 类型的java工具包 。
 * 
 */
public class JMineUtil {

	private static Logger logger = LoggerFactory.getLogger(JMineUtil.class);

	public static String getMine(File file) {
		MagicMatch match = null;
		try {
			match = Magic.getMagicMatch(file, true);
		} catch (Exception e) {
			logger.error("JMineUtil Mime Type MagicMatch error", e);
			return "application/octet-stream";
		}
		logger.info(file.getName() + " Mime Type = " + match.getMimeType());
		return match.getMimeType();
	}
	
	public static String getMine(String filePath) {
		return getMine(new File(filePath));
	}

	public static void main(String[] args) {
		//System.out.println(getMine(new File("d:/no_upload_pic.png")));
		System.out.println(getMine("d:/no_upload_pic.png"));
	}
}